﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace P2P.Main
{
    public class ApiRoute
    {
        private static readonly string routeName = "RouteMap.xml";

        public static Dictionary<string, Api> ApiMap = new Dictionary<string, Api>();

        static ApiRoute()
        {
            Init();
        }

        public static void Init()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(routeName);

            XmlNodeList nodes = doc.SelectNodes("//Api");

            foreach (XmlNode node in nodes)
            {
                XmlNodeList nodeList = node.ChildNodes;

                Api Api = new Api();

                foreach (XmlNode itemNode in nodeList)
                {
                    string value = itemNode.InnerText.Trim();

                    switch (itemNode.Name)
                    {
                        case "Name":
                            Api.Name = value;
                            break;
                        case "Protocol":
                            Api.Protocol = value;
                            break;
                        case "Method":
                            Api.Method.Add(value);
                            break;
                        case "Handler":
                            Api.Handler = value;
                            break;
                        case "Version":
                            Api.Version = value;
                            break;
                        default:
                            break;
                    }
                }
                ApiMap.Add(Api.Name, Api);
            }
        }
    }
}
